package com.ttb.database;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ScheduleDAO {
	private SQLiteDatabase database;
	private DBHandler dbHandler;
	private String[] allColumns = {DBHandler.SCHEDULE_KEY_ID,
			DBHandler.SCHEDULE_KEY_PHONE_LIST,
			DBHandler.SCHEDULE_KEY_MESSAGE,
			DBHandler.SCHEDULE_KEY_TRIGGER_TIME,
			DBHandler.SCHEDULE_KEY_INTERVAL};
	private static final String SCHEDULE_TABLE = DBHandler.SCHEDULE_TABLE;
	private static final String ID = DBHandler.SCHEDULE_KEY_ID;
	private static final String NUM_PHONE_LIST =  DBHandler.SCHEDULE_KEY_PHONE_LIST;
	private static final String MESSAGE = DBHandler.SCHEDULE_KEY_MESSAGE;
	private static final String TRIGGER_TIME =  DBHandler.SCHEDULE_KEY_TRIGGER_TIME;
	private static final String INTERVAL =  DBHandler.SCHEDULE_KEY_INTERVAL;
	public ScheduleDAO(Context context) {
		dbHandler = new DBHandler(context);
	  }

	  public void open() throws SQLException {
	    database = dbHandler.getWritableDatabase();
	  }

	  public void close() {
		  dbHandler.close();
	  }

	  public Schedule createSchedule(String num_phone_list,String message, long triggerTime, String interval) {
	    ContentValues values = new ContentValues();
	    values.put(NUM_PHONE_LIST, num_phone_list);
	    values.put(MESSAGE, message);
	    values.put(TRIGGER_TIME, triggerTime);
	    values.put(INTERVAL, interval);
	    Log.d("Schedule :", num_phone_list + " : " + triggerTime + " : " + interval);
	    long insertId = database.insert(SCHEDULE_TABLE, null,
	        values);
	    if(insertId > 0)
	    {
	    	return getSchedule(insertId);
	    }
	    return null;
	  }
	//get single schedule
		public Schedule  getSchedule(long id) {
			
			Cursor cursor = database.query(SCHEDULE_TABLE, 
					allColumns, 
					ID + "=?", new String[] {String.valueOf(id)}, 
					null, null, null);
			if(cursor != null)
			{
				cursor.moveToFirst();
				
			}
			Schedule schedule = cursorToSchedule(cursor);
			cursor.close();
			return schedule;
		}
	  public void deleteSchedule(Schedule schedule) {
	    long id = schedule.getId();
	    System.out.println("Schedule deleted with id: " + id);
	    database.delete(SCHEDULE_TABLE, ID + " = " + id, null);
	  }

	  public List<Schedule> getAllSchedules() {
	    List<Schedule> schedules = new ArrayList<Schedule>();

	    Cursor cursor = database.query(SCHEDULE_TABLE,
	    		allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	    	Schedule schdule = cursorToSchedule(cursor);
	    	schedules.add(schdule);
	      cursor.moveToNext();
	    }
	    // Make sure to close the cursor
	    cursor.close();
	    return schedules;
	  }
	  
	  private Schedule cursorToSchedule(Cursor cursor) {
		Schedule schedule = new Schedule();
		schedule.setId(cursor.getLong(0));
		schedule.setNumberPhoneList(cursor.getString(1));
		schedule.setMessage(cursor.getString(2));
		schedule.setTriggerTime(cursor.getLong(3));
		schedule.setInterval(cursor.getString(4));
	    return schedule;
	  }
}
